package com.margo.project.append.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.margo.project.append.model.MarColumnNameVO;

@Component
public class MargoBeanPropertyMapper {
	
	Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public List<MarColumnNameVO> queryMarColumnNameList(String tableName){
		StringBuilder sbSQL=new StringBuilder();
		sbSQL.append(" SELECT USER_TAB_COLUMNS.COLUMN_NAME,\n");
		sbSQL.append("        USER_TAB_COLUMNS.DATA_TYPE,\n");
		sbSQL.append("        USER_TAB_COLUMNS.DATA_LENGTH,\n");
		
		sbSQL.append("        USER_TAB_COLUMNS.DATA_PRECISION,\n");
		sbSQL.append("        USER_TAB_COLUMNS.DATA_SCALE,\n");
		sbSQL.append("        USER_TAB_COLUMNS.NULLABLE,\n");
		
		sbSQL.append("        USER_COL_COMMENTS.COMMENTS\n");
		sbSQL.append("   FROM USER_TAB_COLUMNS, USER_COL_COMMENTS\n");
		sbSQL.append("  WHERE USER_TAB_COLUMNS.COLUMN_NAME = USER_COL_COMMENTS.COLUMN_NAME\n");
		sbSQL.append("    AND USER_TAB_COLUMNS.TABLE_NAME = USER_COL_COMMENTS.TABLE_NAME\n");
		sbSQL.append("    AND USER_TAB_COLUMNS.TABLE_NAME = ? \n");
		sbSQL.append("  ORDER BY COLUMN_ID");
		Object[] param=new Object[1];
		param[0]=tableName;	
		RowMapper<MarColumnNameVO> rowMapper = new BeanPropertyRowMapper<MarColumnNameVO>(MarColumnNameVO.class);
		List<MarColumnNameVO> lstItem=jdbcTemplate.query(sbSQL.toString(), param,rowMapper);
		for(MarColumnNameVO marColumnNameVO:lstItem) {
			logger.info(marColumnNameVO.toString());
		}
		return lstItem;
	}
}
